package com.zcf.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zcf.common.json.Body;
import com.zcf.common.utils.DateUtils;
import com.zcf.common.utils.MD5Util;
import com.zcf.mapper.CodeMapper;
import com.zcf.mapper.UserMapper;
import com.zcf.pojo.Code;
import com.zcf.pojo.Log;
import com.zcf.pojo.User;
import com.zcf.service.UserService;
import com.zcf.utils.Hutool;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CodeMapper codeMapper;

	public Body register(String phone, String password, String code) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		User user = new User();
		user.setPhone(phone);
		User userResult = userMapper.selectOne(user);
		if (userResult != null) {
			return Body.newInstance(501, "手机号已存在");
		}
		// 验证码验证
		if (code != null) {
			EntityWrapper<Code> ew = new EntityWrapper<>();
			ew.eq("code", code);
			int flag = codeMapper.selectCount(ew);
			if (flag == 0) {
				return Body.newInstance(501, "验证码不正确");
			}
			// 如果不是第三方登录 用户名默认为手机号
			user.setId(Hutool.getId());
			user.setName(Hutool.getRandomString(8));
			user.setPhone(phone);
			user.setTime(time);
			user.setRole(0);
			user.setHead("http://hbtianshou.com/images/images/2f25ba30c762454a9e3281f4ea87cfe7.jpeg");
			// MD5加密
			user.setPassword(MD5Util.MD5EncodeUtf8(password));
			Integer insert = userMapper.insert(user);
			if (insert > 0) {
				return Body.newInstance(user);
			}
			return Body.newInstance(501, "注册失败");
		}
		return Body.newInstance(501, "验证码有误");
	}

	public Body login(String phone, String password) {
		String md5EncodeUtf8 = MD5Util.MD5EncodeUtf8(password);
		User user = new User();
		user.setPhone(phone);
		User one = userMapper.selectOne(user);
		if (one != null && !"".equals(one)) {
			String pass = one.getPassword();
			if (md5EncodeUtf8.equals(pass)) {
				one.setLine(1);
				one.updateById();
				return Body.newInstance(one);
			}
			return Body.newInstance(501, "密码错误");
		}
		return Body.newInstance(501, "账户输入有误");
	}

	public Body forgetPassword(String phone, String newPassword, String code) {
		String pass = MD5Util.MD5EncodeUtf8(newPassword);
		User user = new User();
		user.setPhone(phone);
		User param = userMapper.selectOne(user);
		if (param != null && !"".equals(param)) {
			if (code != null) {
				EntityWrapper<Code> ew = new EntityWrapper<>();
				ew.eq("code", code);
				int flag = codeMapper.selectCount(ew);
				if (flag == 0) {
					return Body.newInstance(501, "验证码不正确");
				}
				param.setPassword(pass);
				userMapper.updateById(param);
				return Body.newInstance(param);
			}
			return Body.newInstance(501, "验证码不能为空");
		}
		return Body.newInstance(501, "账号有误");
	}

	public Body changePhone(String phone, String newPhone, String code) {
		User user = new User();
		user.setPhone(phone);
		User one = userMapper.selectOne(user);
		if (one != null && !"".equals(one)) {
			if (code != null) {
				EntityWrapper<Code> ew = new EntityWrapper<>();
				ew.eq("code", code);
				int flag = codeMapper.selectCount(ew);
				if (flag == 0) {
					return Body.newInstance(501, "验证码不正确");
				}
				one.setPhone(newPhone);
				userMapper.updateById(one);
				return Body.newInstance(one);
			}
			return Body.newInstance(501, "验证码不能为空");

		}
		return Body.newInstance(501, "手机号有误");
	}

	public static void main(String[] args) {
		System.out.println(MD5Util.MD5EncodeUtf8("123456"));
	}

	public Body changePassword(String userId, String phone, String password, String newPassword) {
		User user = new User();
		user.setId(userId);
		User one = user.selectById();
		String md5Password = MD5Util.MD5EncodeUtf8(password);
		if (one != null && !"".equals(one)) {
			System.out.println(password);
			System.out.println(one.getPassword() + "========" + md5Password);
			if (one.getPassword().equals(md5Password)) {
				one.setPassword(MD5Util.MD5EncodeUtf8(newPassword));
				userMapper.updateById(one);
				return Body.newInstance(one);
			}
			return Body.newInstance(501, "原密码输入有误");
		}
		return Body.newInstance(501, "用户不存在");
	}

	@Override
	public Body login(String wx_id, String nick_name, String head_img) {
		EntityWrapper<User> u = new EntityWrapper<User>();
		u.eq("wx_id", wx_id);
		User uu = this.selectOne(u);
		if (uu != null) {
			uu.setLine(1);
			uu.updateById();
			return Body.newInstance(uu);
		} else {
			User user = new User();
			user.setName(nick_name);
			user.setHead(head_img);
			user.setRole(0);
			user.setWxId(wx_id);
			user.setId(Hutool.getId());
			user.setTime(DateUtils.formatTimeNow());
			user.setStatus(0);
			user.setCount(1);
			user.setLine(1);
			boolean b = this.insert(user);
			if (b) {
				return Body.newInstance(user);
			} else {
				return Body.BODY_500;
			}
		}
	}

}
